package in.muton.edushare.poll.components;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.poll.pages.PollAnswer;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.Poll;

public class VoteLink extends Link<IModel<Poll>> {

	private static final long serialVersionUID = 7359376599145927452L;

	public VoteLink(String id, IModel<Poll> model) {
		super(id);
		pollModel = model;
	}

	@Override
	public void onClick() {
		PageParameters p = new PageParameters();
		p.add("0", Integer.toString(pollModel.getObject().getPollId()));
		setResponsePage(PollAnswer.class, p);
	}

	@Override
	public boolean isVisible() {
		UserPollDAO upDAO = new UserPollDAO(EduShareSession.get()
				.getSessionORMInterface());
		boolean isRespondent = upDAO.getPollRespondents(pollModel.getObject())
				.contains(EduShareSession.get().getLoggedInUser());
		boolean isPollOpen = pollModel.getObject().getStatus() == Poll.POLL_STATUS_POLLING;
		return isRespondent && isPollOpen;
	}

	private IModel<Poll> pollModel = null;
}
